package com.vg.leftmenu;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;

import com.androidhive.jsonparsing.JSONParser;
import com.androidhive.jsonparsing.MyAdapter;
import com.androidhive.jsonparsing.R;
import com.vg.config.linkUrl;
import com.vg.service.ServiceBody;
import com.vg.service.ServiceCity;
import com.vg.service.ServiceColor;
import com.vg.service.ServiceDrinker;
import com.vg.service.ServiceEducation;
import com.vg.service.ServiceGender;
import com.vg.service.ServiceMaritalStatus;
import com.vg.service.ServiceMyHeight;
import com.vg.service.ServiceMyWeight;
import com.vg.service.ServiceReligion;
import com.vg.service.ServiceSmoker;
import com.vg.service.ServiceUserType;
import com.vg.spinner.Student;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class chinhsuathongtin extends Activity {
	MyAdapter adapter;
	JSONParser jparse;
	JSONArray jsa;
	linkUrl mUrl;
	
	ServiceBody svBody;
	ServiceCity svCity;
	ServiceColor svColor;
	ServiceDrinker svDrinker;
	ServiceEducation svEducation;
	ServiceGender svGender;
	ServiceMaritalStatus svMaritalStatus;
	ServiceMyHeight svMyHeight;
	ServiceMyWeight svMyWeight;
	ServiceReligion svReligion;
	ServiceSmoker svSmoker;
	ServiceUserType svUserType;
	
	private ArrayList<Student> array;
	
	String strBody, strCity, strColor, strDrinker, strEducation, strGender, strMaritalStt,
			strHeight, strWeight, strReligion, strSmoker, strUserType;
	
	private static final String PHRASE = "Item_";
	private String prefBody = "PrefBody";
	private String prefCity = "PrefCity";
	private String prefColor = "PrefColor";
	private String prefDrinker = "PrefDrinker";
	private String prefEducation = "PrefEducation";
	private String prefGender = "PrefGender";
	private String prefMaritalStatus = "PrefMaritalStatus";
	private String prefHeight = "PrefHeight";
	private String prefWeight = "PrefWeight";
	private String prefReligion = "PrefReligion";
	private String prefSmoker = "PrefSmoker";
	private String prefUsertype = "PrefUsertype";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_edit_infomation);
		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		EditText etName, etBirthday, etAvatar, etPhone;
		Button btnAvatar, btnOk, btnCancel;
		final Spinner spGender, spCity, spUserType, spReligion, spEducation,spSmoker,
				spDrinker, spMaritalStatus, spBodyType, spColor, spHeight, spWeight;
		
		
		
		etName = (EditText) findViewById(R.id.edit_infomation_etName);
		etBirthday = (EditText) findViewById(R.id.edit_infomation_etBirthday);
		etAvatar = (EditText) findViewById(R.id.edit_infomation_etAvatar);
		etPhone = (EditText) findViewById(R.id.edit_infomation_etPhone);
		btnAvatar = (Button) findViewById(R.id.edit_infomation_btnAvatar);
		btnOk = (Button) findViewById(R.id.edit_infomation_btnOk);
		btnCancel = (Button) findViewById(R.id.edit_infomation_btnCancel);
		spGender = (Spinner) findViewById(R.id.spinner_gender);
		spCity = (Spinner) findViewById(R.id.spinner_city);
		spUserType = (Spinner) findViewById(R.id.spinner_userType);
		spReligion = (Spinner) findViewById(R.id.spinner_religion);
		spEducation = (Spinner) findViewById(R.id.spinner_education);
		spSmoker = (Spinner) findViewById(R.id.spinner_smoker);
		spDrinker = (Spinner) findViewById(R.id.spinner_drinker);
		spMaritalStatus = (Spinner) findViewById(R.id.spinner_maritalStatus);
		spBodyType = (Spinner) findViewById(R.id.spinner_bodyType);
		spColor = (Spinner) findViewById(R.id.spinner_color);
		spHeight = (Spinner) findViewById(R.id.spinner_height);
		spWeight = (Spinner) findViewById(R.id.spinner_weight);
		
		mUrl = new linkUrl();
		
		dropItems(spBodyType, prefBody, dropSpinner(prefBody));
		dropItems(spCity, prefCity, dropSpinner(prefCity));
		dropItems(spColor, prefColor, dropSpinner(prefColor));
		dropItems(spDrinker, prefDrinker, dropSpinner(prefDrinker));
		dropItems(spEducation, prefEducation, dropSpinner(prefEducation));
		dropItems(spGender, prefGender, dropSpinner(prefGender));
		dropItems(spHeight, prefHeight, dropSpinner(prefHeight));
		dropItems(spMaritalStatus, prefMaritalStatus, dropSpinner(prefMaritalStatus));
		dropItems(spReligion, prefReligion, dropSpinner(prefReligion));
		dropItems(spSmoker, prefSmoker, dropSpinner(prefSmoker));
		dropItems(spUserType, prefUsertype, dropSpinner(prefUsertype));
		dropItems(spWeight, prefWeight, dropSpinner(prefWeight));
		
		
		btnAvatar.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
			//	Intent intent = new Intent(chinhsuathongtin.this, ChangeAvatarActivity.class);
			//	startActivity(intent);
			}
		});
	}
	
//	public void putPref(String pref, String url) {
//		jparse = new JSONParser();
//		jsa = jparse.getJsonarray(url);
//		SharedPreferences prefs;
//		prefs = getSharedPreferences(pref, MODE_PRIVATE);
//		SharedPreferences.Editor editor = prefs.edit();
//		for (int i = 0; i < jsa.length(); i++) {
//			try {
//				editor.putString(PHRASE + i + "", jsa.getString(i));
//			} catch (JSONException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}
//		editor.commit();
//	}
	
	public void dropItems(Spinner sp, String pref, ArrayList<Student> arr) {
		array = new ArrayList<Student>();
		adapter = new MyAdapter(this, R.layout.drop_gender, arr, true);
		sp.setAdapter(adapter);
		SharedPreferences prefs;
		prefs = getSharedPreferences(pref, MODE_PRIVATE);
		for(int i = 0; i < prefs.getAll().size(); i++) {    
            Student n = new Student(prefs.getString(PHRASE + i, ""));
            array.add(n);
        }
		adapter.notifyDataSetChanged();
	}
	
	public ArrayList<Student> dropSpinner(String pref) {
		ArrayList<Student> arrayList=new ArrayList<Student>();
		SharedPreferences prefs;
		prefs = getSharedPreferences(pref, MODE_PRIVATE);
		for(int i = 0; i < prefs.getAll().size(); i++) {    
            Student n = new Student(prefs.getString(PHRASE + i + "", ""));
            arrayList.add(n);
        }
		return arrayList;
	}
}
